<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout = '//layouts/column3';
	
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	//public $menu = array();
	
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs = array();
	
    /**
     * Use in tab menu and side bar. 
     * Array of categories. Each $category is an aray which is in form that:
     *     array('id'       => %TYPE_ID%,
     *           'name'     => %TYPE_NAME%,
     *           'childCategories' => array(
     *                   array('id'   => %CATEGORY_ID1%, 
     *                           'name' => %CATEGORY_NAME1%),
     *                   array('id'   => %CATEGORY_ID2%, 
     *                           'name' => %CATEGORY_NAME2%)));
     * @var array
     */
    public $categories = array();
    public $subCategories = array();
    
    /**
     * 
     * @var array
     */
    public $languages = array('default' => 'vi', 'supported' => array('vi', 'en'));
    
    /**
     * 
     */
    public function __construct($id, $module=NULL) {
        parent::__construct($id, $module);
        
        $this->initLanguage();
        $this->initCategories(Yii::app()->language);
    }
    
    /**
     * 
     */
    public function initLanguage() {
        $lang = Yii::app()->session['language'];
        if (isset($lang) && in_array($lang, $this->languages['supported']) ) {
            Yii::app()->language = $lang;
        } else {
            Yii::app()->language = $this->languages['default'];
        }
    }
    
    /**
     * 
     * @param unknown_type $language
     */
    public function initCategories($language) {
        $results = Yii::app()->db->createCommand()
            ->selectDistinct('c.id id , cd.name name')
            ->from('category c')
            ->leftJoin('category_detail cd', 'c.id = cd.category_id')
            ->where('cd.language = :language and c.parent_id is null', 
                    array(':language' => $language))
            ->queryAll();
        if (count($results) == 0) {
            return;
        }
        
        $categories = array();
        foreach($results as $category) {
            $cats = Yii::app()->db->createCommand()
                ->selectDistinct('c.id id , cd.name name')
                ->from('category c')
                ->leftJoin('category_detail cd', 'c.id = cd.category_id')
                ->where('cd.language = :language and c.parent_id = :parent_id', 
                        array(':language' => $language, ':parent_id' => $category['id']))
                ->queryAll();
            
            if (count($cats) > 0) {
                $category['childCategories'] = $cats;
            }else{
                $category['childCategories'] = null;
            }   
            $categories[] = $category;
        }       
        $this->categories = $categories;
    }
}